package com.book.service.impl;

import com.book.api.RankingApi;
import com.book.pojo.Reward;
import com.book.mapper.RewardDao;
import com.book.pojo.RewardRanking;
import com.book.service.RewardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fk
 * @since 2023-10-14 11:34:14
 */
@Service
public class RewardServiceImpl extends ServiceImpl<RewardDao, Reward> implements RewardService {

    @Autowired
    private RewardDao rewardDao;
    @Autowired
    private RankingApi rankingApi;

    @Override
    public List<Reward> RewardRanking() {
        return rewardDao.RewardRanking();
    }

    @Override
    public void addReward(Reward reward) {
        rewardDao.insert(reward);
        Long bookId = reward.getBookId();
        RewardRanking ranking = new RewardRanking();
        rankingApi.deleteRewardRankingByBookId(bookId);
        Reward r = rewardDao.getRewardByBookId(bookId);
        ranking.setBookId(r.getBookId());
        ranking.setRewardSum(r.getRewardSum());
        rankingApi.addRewardRanking(ranking);
    }
}
